# src/models/Market.py
from . import db
import datetime
from marshmallow import fields, Schema

class MarketModel(db.Model):
  """
  Market Model
  """

  __tablename__ = 'markets'

  id = db.Column(db.Integer, primary_key=True)
  mtName = db.Column(db.String(20), nullable=False)
  mtPre = db.Column(db.String(20), nullable=False)


  def __init__(self, data):
    self.mtName = data.get('mtName')
    self.mtPre = data.get('mtPre')


  def save(self):
    db.session.add(self)
    db.session.commit()

  def update(self, data):
    for key, item in data.items():
      setattr(self, key, item)
    db.session.commit()

  def delete(self):
    db.session.delete(self)
    db.session.commit()
  
  @staticmethod
  def get_all_markets():
    return MarketModel.query.all()

  @staticmethod
  def get_one_market(id):
    return MarketModel.query.get(id)

  def __repr__(self):
    return '<id {}>'.format(self.id)

class MarketSchema(Schema):
  """
  Market Schema
  """
  id = fields.Int(dump_only=True)
  mtName = fields.Str(required=True)
  mtPre = fields.Str(required=True)

